草庐IT

SQL EXISTS 运算符

全部标签

c++ - `using` 用户定义文字运算符的声明

是否可以为文字运算符operator""声明using?例如,#includenamespaceMyNamespace{constexprstd::chrono::hoursoperator""_hr(unsignedlonglongn){returnstd::chrono::hours{n};}//...otherstuffinthenamespace...}usingMyNamespace::operator"";//DOESNOTCOMPILE!intmain(){autofoo=37_hr;}我的解决方法是将这些运算符放在它们自己的嵌套命名空间中,称为literals,这允许u

c++ - 运算符 T() 未在赋值中使用

我对此有点困惑。假设我有一个辅助类DataclassData{public:Data(constQVariant&value):m_Variant(value){}operatorQString()const{returnm_Variant.toString();}private:QVariantm_Variant;};然后当我这样做时:Datad("text");QStringstr=d;//strbecomes"text"它有效,但当我继续这样做时:Datad2("text2");str=d2;//doesnotcompile提示失败:ambiguousoverloadfor'op

c++对模板类的用户定义运算符的隐式转换

我有一个结构模板A和一个+运算符int.#includetemplatestructA{inta;};templateintoperator+(Aa,intb){returna.a+b;}我创建了一个结构模板B,可转换为A.templatestructB{intb=3;operatorA(){return{b+10};}};现在我要B转换为A打电话时B+int.intmain(){std::cout{9}+10){9}+10)我读了Implicitconversionwhenoverloadingoperatorsfortemplateclasses并写道templatestructB

c++ - 具有相同底层类类型的条件运算符

这个程序应该输出0还是1?在我阅读和理解C++14标准中引用的段落时,它应该打印1,但GCC和clang都打印0(因为推导类型是Aconst而不是A常量&):#includestructA{};intmain(){Aa;Aconst&ra=std::move(a);//#1std::cout::value;//Prints0}在这种情况下,ra是一个Aconst左值,而std::move(a)是一个Axvalue,都是类类型。根据有关条件运算符的标准(重点是我的),结果应该是Aconst类型的lvalue,因此decltype结果必须是Aconst&:[expr.cond]/3Othe

c++ - 双 bool 否定运算符

这个问题在这里已经有了答案:DoubleNegationinC++(14个答案)doublenegationinC:isitguaranteedtoreturn0/1?(2个答案)关闭4年前。我从MicrosoftimplementationofGSL中看到这段代码(C++指南支持库):#ifdefined(__clang__)||defined(__GNUC__)#defineGSL_LIKELY(x)__builtin_expect(!!(x),1)#defineGSL_UNLIKELY(x)__builtin_expect(!!(x),0)#else#defineGSL_LIKE

c++ - 运算符删除导致堆损坏,而运算符新工作正常

我已经让operatornew工作了,但是当我调用delete时,它​​在free(ptr)行崩溃了。任何人都可以告诉我在这个基类中重载operatornew和delete时我做错了什么吗?提示:我不是在问设计问题。classBase{private:inti;public:Base():i(10){}staticvoid*operatornew(size_tsize){if(size=0)size=1;//pleasereadthislinecarefully!size=0!returnmalloc(size);}staticvoidoperatordelete(void*ptr,s

c++ - 列表中 -> 运算符的语义(以及一般的 C++)

我目前的任务是编写一个带有迭代器的列表。与创建迭代器类一样,列表不是问题。从几个来源,我看到我有两个运算符要在我的迭代器类中定义:operator*和operator->。到目前为止太棒了!假设我的迭代器结构是这样的//NestedclassofListclass_Iter{private:ListElem*pCurr;constList*pList;public:_Iter(ListElem*pCurr,constList*list):pCurr_(pCurr),pList(list){}T&operator*(){returnpCurr_->data;}T*operator->()

c++ - std::ostringstream 运算符重载搜索顺序?

我有以下类(class):namespace{classMimeLogger:publicstd::ostringstream{public:MimeLogger(){}~MimeLogger(){LOGEVENT(logModuleWSE,logEventDebug,logMsgWSETrace1,str());}};}当我这样做时:MimeLogger()第一个"Hello"字符串被视为void*.如果我调试代码,"Hello"传递到std::basic_ostream::operator并打印为指针值,而不是字符串。第二个字符串,"World"被正确地传递到全局重载charcon

c++ - 波浪号运算符分别返回 -1、-2 而不是 0、1

我对此有些疑惑。我认为C++中的~运算符应该以不同的方式工作(不是Matlab-y)。这是一个最小的工作示例:#includeusingnamespacestd;intmain(intargc,char**argv){boolbanana=true;boolpeach=false;cout这是我的输出:1-20-1我希望有人对此有所了解。 最佳答案 这正是应该发生的事情:当你反转零的二进制表示时,你得到负一;当您反转1的二进制表示时,您会得到二进制补码表示中的负2。00000000-->~-->11111111//Thisis-10

c++重载另一个类的赋值运算符

我有一个C++类来处理分数,我希望它允许转换为double,我有类似的东西:classfraction{doublen,d;public:fraction(double_n,double_d){n=_n;d=_d;}//somefunctionsdoubletodouble(){returnn/d;}};fractionfrac(1,2);doubledbl=frac.todouble();效果很好,但我想重载赋值运算符,这样我就可以直接使用:doubledbl=frac;我试着添加这个:doubledouble::operator=(double&dbl,fraction&frac)